package com.huangyi.adminservice.house.service.filter;


import com.huangyi.adminapi.house.domain.dto.SearchHouseListReqDTO;
import com.huangyi.adminservice.house.domain.dto.HouseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 租金范围筛选策略实现类
 * 
 * <p>根据租金价格范围对房源进行筛选，支持多个价格区间同时筛选
 * 
 * @author huangyi
 * @version 1.0
 * @since 2024
 */
@Slf4j
@Component
public class RentalRangesFilter implements IHouseFilter {

    /**
     * 根据租金范围过滤房源
     * 
     * @param houseDTO 待过滤的房源信息
     * @param reqDTO 搜索请求参数，包含租金范围筛选条件
     * @return true-通过过滤，false-被过滤掉，null-不进行过滤
     */
    @Override
    public Boolean filter(HouseDTO houseDTO, SearchHouseListReqDTO reqDTO) {
        return CollectionUtils.isEmpty(reqDTO.getRentalRanges())
                || filterHouseByRentalRanges(houseDTO.getPrice(), reqDTO.getRentalRanges());
    }

    /**
     * 根据租金范围筛选房源
     * 
     * <p>支持的价格区间：
     * <ul>
     *   <li>range_1: < 1000元</li>
     *   <li>range_2: 1000-1499元</li>
     *   <li>range_3: 1500-1999元</li>
     *   <li>range_4: 2000-2999元</li>
     *   <li>range_5: 3000-4999元</li>
     *   <li>range_6: >= 5000元</li>
     * </ul>
     * 
     * @param price 房源价格
     * @param rentalRanges 租金范围列表
     * @return true-价格在指定范围内，false-价格不在指定范围内
     */
    private boolean filterHouseByRentalRanges(Double price, List<String> rentalRanges) {
        if (null == price) {
            return false;
        }

        boolean isPriceInRange = false;
        for (String rentalRange : rentalRanges) {
            switch (rentalRange) {
                case "range_1":
                    isPriceInRange = price < 1000;
                    break;
                case "range_2":
                    isPriceInRange = price >= 1000 && price < 1500;
                    break;
                case "range_3":
                    isPriceInRange = price >= 1500 && price < 2000;
                    break;
                case "range_4":
                    isPriceInRange = price >= 2000 && price < 3000;
                    break;
                case "range_5":
                    isPriceInRange = price >= 3000 && price < 5000;
                    break;
                case "range_6":
                    isPriceInRange = price >= 5000;
                    break;
                default:
                    log.error("超出资金筛选范围, rentalRange:{}", rentalRange);
                    break;
            }
            if (isPriceInRange) {
                return true;
            }
        }
        return false;
    }

}
